package com.example.pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

public class InternalPage extends AnyPage {

    @FindBy(xpath = "//nav/ul/li[1]")
    private WebElement homeLink;

    @FindBy(xpath = "//nav/ul/li[2]")
    private WebElement profileLink;

    @FindBy(xpath = "//nav/ul/li[3]")
    private WebElement usersLink;

    @FindBy(xpath = "//nav/ul/li[4]")
    private WebElement logoutLink;

    public boolean isMenuItemSelected(WebElement webElement) {
        return webElement.getAttribute("class").contains("selected");
    }

    @Override
    public boolean isOnThisPage() {
        return logoutLink.isDisplayed();
    }

    void tryToOpen() {
        MyPageFactory.getPage(driver, LoginPage.class).loginWithValidCredentials("admin", "admin");
    }


    public LoginPage logout() {
        logoutLink.click();
        wait_().until(alertIsPresent()).accept();
        return MyPageFactory.getPage(driver,LoginPage.class);
    }

    public void goToHome() {
        homeLink.click();
    }

    public void goToProfile() {
        profileLink.click();
    }

    public UsersPage goToUsers() {
        usersLink.click();
        return MyPageFactory.getPage(driver, UsersPage.class);
    }

}
